/**
 *http请求相关对象 支持get post请求
 *
 */
var HTTPWorker = new  (function(){

var emptyFn = function() {};
/**
 *
 * @param {Object} actionName 接口名称
 * @param {Object} method 方法
 * @param {Object} params 参数
 * @param {Object} onSuccessFunction 成功回调
 * @param {Object} onFailedFunction 失败回调
 */
this.sendRequest = function(action, method, params, onSuccessFunction, onFailedFunction) {

	var that = this;
	var timestamp = new Date().getTime();
	var path = action +  "?timestap=" + timestamp;
	params = params || {};
	$.ajax({
		url: path,
		type: method,
		data:params,
		xhrFields: {
	    	withCredentials: true
	    },
		crossDomain: true,
		error: function(jqXHR, textStatus, errorThrown) {

			console.log("请求异常！");
		},
		success: function(json, textStatus, jqXHR) {

			that.handleResultJSON(JSON.parse(json), onSuccessFunction);
		}
	});
}
//处理响应想结果
this.handleResultJSON = function(json, onSuccessFunction) {

	var httpResult = new HTTPResult();
	httpResult.msg = json.message;
	httpResult.result = json.code;


	if(json.data){
		/*httpResult.data =  JSON.parse(json.data);	*/
		httpResult.data =  json.data;
	}

	var c = onSuccessFunction || emptyFn;
	if(c(httpResult)){
		return;
	};



	if (!httpResult.isSuccess()) {
		var msg = httpResult.msg ? httpResult.msg : "访问失败";
		alert(msg);


	}

}

/***
	* 发送网络请求
	* @param {Object} actionName 请求的actionname
	* @param {Object} onSuccessFunction
	* @param {Object} onFailedFunction
	* @param {Object} params 参数
*/
this.postRequest = function(actionName, params, onSuccessFunction, onFailedFunction) {
	this.sendRequest(actionName, "POST", params, onSuccessFunction, onFailedFunction);
}

this.getRequest = function(actionName, params, onSuccessFunction, onFailedFunction) {

	this.sendRequest(actionName, "GET", params, onSuccessFunction, onFailedFunction);
}

this.postEmptyParamerRequest = function(url, onSuccessFunction, onFailedFunction) {
	this.postRequest(url, null, onSuccessFunction, onFailedFunction);

}

/***
	* 提交表单函数 支持多文件上传
	* @param {Object} form 提交的表单对象（jquery)
	* @param {Object} url 请求地址
    * @param {Object} params 参数
	* @param {Object} onSuccessFunction
	* @param {Object} onFailedFunction
*/
this.submitForm = function(form, url, params, onSuccessFunction, onFailedFunction) {



	var that = this;

	var data = new FormData(form);

	$.each(params || {}, function(key, value) {
		data.append(key, value);
	});

	$.ajax({
		url: url,
		type: "post",
		dataType: "JSON",
		data: data,
		processData: false,
		contentType: false,
		success: function(data, status) {


			that.handleResultJSON(data, onSuccessFunction, onFailedFunction);
		},
		error: function(xhr, desc, err) {

			console.log("请求错误");



		}
	});

}

})()


//HTTPWorker get post 响应的结果
var HTTPResult = function(){
	this.result;
	this.msg;
	this.data;
	this.resultJson;
	//判断网络请求是否成功
	this.isSuccess = function() {
		return parseInt(this.result) == 10200;
	}
}

//HTTPRouter 接口地址
var HTTPRouter = new  (function(){
	this.ApiUrl = "http://scale-b.yoparent.cn/yoparent/scale/pospsy/";
	//api版本号
	this.ApiVer = "V1.0";
	this.getResult = this.ApiUrl + "result"; //提交测评
	this.getWXInfoUrl = "http://scale-b.yoparent.cn/yoparent/scale/initData";

	this.updateAssessmentState = "http://app.v.yoparent.cn/alading/user/updateAssessmentState";
})();
